package com.example.hospitalsystem.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hospitalsystem.mapper.NoticeMapper;
import com.example.hospitalsystem.pojo.dto.notice.NoticePageQueryDto;
import com.example.hospitalsystem.pojo.entity.Notice;
import com.example.hospitalsystem.result.Result;
import com.example.hospitalsystem.service.NoticeService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Override
    public Result<Object> getNewest() {

        LambdaQueryWrapper<Notice> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Notice::getPublishTime).last("LIMIT 1");
        Notice notice=baseMapper.selectOne(lambdaQueryWrapper);
        return Result.build(notice,200,"返回最新通知信息成功");
    }

    @Override
    public Result<Object> getTopTen() {
        LambdaQueryWrapper<Notice> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Notice::getPublishTime).last("LIMIT 10");
        List<Notice> noticeList=baseMapper.selectList(lambdaQueryWrapper);
        return Result.build(noticeList,200,"返回前10条通知信息成功");
    }

    @Override
    public Result<Object> deleteNotice(Integer id) {
        LambdaQueryWrapper<Notice> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Notice::getNoticeId,id);
        baseMapper.delete(lambdaQueryWrapper);

        return Result.build("删除通知信息成功");
    }

    @Override
    public Result<Object> getAllNotice(NoticePageQueryDto noticePageQueryDto) {
        Page<Notice> noticePage=new Page<>(noticePageQueryDto.getPageNum(),noticePageQueryDto.getPageSize());
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Notice::getPublishTime);

        if(noticePageQueryDto.getContent()==null||noticePageQueryDto.getContent()==""){
            IPage<Notice> noticeIPage=baseMapper.selectPage(noticePage,lambdaQueryWrapper);
            return Result.build(noticeIPage, 200, "返回所有通知信息成功");

        }else{
            lambdaQueryWrapper.like(Notice::getContent,"%"+noticePageQueryDto.getContent()+"%");
            IPage<Notice> noticeIPage=baseMapper.selectPage(noticePage,lambdaQueryWrapper);
            return Result.build(noticeIPage, 200, "返回所有通知信息成功");

        }
    }
}
